package Frame.common;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Point;
import java.util.Iterator;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

import Frame.librarianFrame.BorrowingInformationFrame;
import controller.Controller;
import table.BarCodeAndBook;
import table.BorrowingInformationAll;

public class AllUserBorrowingInformation extends JFrame {
	private JPanel contentPane;
	private Controller controller;
	private JTable borrowInformationAll;
	private JScrollPane borrowInformationTable;
	public AllUserBorrowingInformation(Controller controller) {
		this.controller = controller;
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.setSize(1920, 971);
		contentPane.add(getBorrowTablePanel(controller.getLibrarianService().borrowQuery("X")));
		contentPane.add(Label1());
		contentPane.add(getJButton1());
	}
	public JButton getJButton1() {
		JButton bt1 = new JButton("续借");
		bt1.setIcon(new ImageIcon(BorrowingInformationFrame.class.getResource("/logo/续借.png")));
		bt1.setFont(new Font("华文楷体", Font.PLAIN, 20));
		bt1.addActionListener(controller);
		bt1.setActionCommand("renewByUser");
		bt1.setBounds(905, 792, 119, 35);
		return bt1;
	}
	public JLabel Label1() {
		JLabel lb1 = new JLabel("欢迎查询您的图书借阅记录");
		lb1.setFont(new Font("华文楷体", Font.PLAIN, 25));
		lb1.setBounds(820, 94, 309, 27);
		return lb1;
	}
	public JScrollPane getBorrowTablePanel(List<BorrowingInformationAll> borrowingInformationAllList) {
		Object dataName[] = { "用户名", "姓名", "书名", "图书编号", "借书日期", "应还书日期"};
		Object data[][] = new Object[borrowingInformationAllList.size()][8];
		Iterator<BorrowingInformationAll> iter = borrowingInformationAllList.iterator();
		int i = 0;
		while (iter.hasNext()) {
			BorrowingInformationAll all = iter.next();
			data[i][0] = all.getLoginName(); 
			data[i][1] = all.getUserName();
			data[i][2] = all.getBookName();
			data[i][3] = all.getBarCodeId();
			data[i][4] = all.getBorrowingDate();			
			data[i][5] = all.getReturnDate();
			i++;
		} 	
		borrowInformationAll = new JTable(data, dataName) {
			@Override
		    public Component prepareRenderer(TableCellRenderer renderer, int row, int column){//设置鼠标光标划过时的颜色改变
	        Component comp = super.prepareRenderer(renderer,row,column);
		        Point p = getMousePosition();
		    	
		        if(p!=null){
		            int rowUnderMouse = rowAtPoint(p);
		            if(rowUnderMouse == row){
		                  comp.setBackground(new Color(223,240,216));
		           }
//		            else{
//		                  comp.setBackground(DefaultLookup.getColor(this,ui,"Table.alternateRowColor"));
//		           }
		        }		        
		        return comp;
		    }
			private static final long serialVersionUID = 1L;
			public boolean isCellEditable(int row, int column) {
				return false; 
			};// 将表设置成无法修改
			
		}; 
		borrowInformationAll.addMouseMotionListener(controller.getMyTableMouseMotion());
		borrowInformationAll.setSelectionBackground(new Color(223,240,216));//设置选中的行的颜色
		TableBackGround.setColumnColor(borrowInformationAll);//设置表的交替行数的颜色
		borrowInformationAll.getTableHeader().setFont(new Font("华文楷体", Font.PLAIN, 20)); // 表头字体
		borrowInformationAll.setFont(new Font("华文楷体", Font.PLAIN, 21)); // 表体字体
		borrowInformationAll.getTableHeader().setBackground(new Color(184,207,240));//设置表头的颜色
		borrowInformationAll.getTableHeader().setPreferredSize(new Dimension(15, 35)); // 表头高度 先后为宽 高
		DefaultTableCellRenderer cr = new DefaultTableCellRenderer();// 将表体的内容设置为居中显示
		borrowInformationAll.setDefaultRenderer(Object.class, cr);
		borrowInformationAll.setRowHeight(borrowInformationAll.getRowHeight() + 15); // 表体高度
		borrowInformationAll.setRowMargin(5);// 设置相邻行中的单元格之间的空白空间量
		//borrowInformation.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // 表格只能选择一行
		borrowInformationAll.repaint();
		borrowInformationAll.addMouseListener(controller);
		borrowInformationAll.setName("getRenewBookByUser");
		borrowInformationTable = new JScrollPane(borrowInformationAll);
		borrowInformationTable.getViewport().setBackground(Color.WHITE);//将JScrollPane设置成白色
		borrowInformationTable.setLocation(0, 195);
		borrowInformationTable.setSize(1921, 544);	
		return borrowInformationTable;
	}
	public void showSearchResult(List<BorrowingInformationAll> result) {
		contentPane.remove(borrowInformationTable);
		contentPane.add(getBorrowTablePanel(result));
		contentPane.validate();
	}
	public JPanel getContentPane() {
		return contentPane;
	}
	public void setContentPane(JPanel contentPane) {
		this.contentPane = contentPane;
	}
	public JTable getBorrowInformationAll() {
		return borrowInformationAll;
	}
	public void setBorrowInformationAll(JTable borrowInformationAll) {
		this.borrowInformationAll = borrowInformationAll;
	}

}
